#!/usr/bin/perl -w
#
# Show an user with associated information
#

use strict;

use Counter;
use Fcntl;
use Getopt::Long;
use Algorithm::Diff;

use vars qw{
    $file $preen $filter $key
    $persons $users $pers $user $field
    $showhistory
    };

$showhistory = 0;

$users = Counter::open();

GetOptions(
	   "history" => \$showhistory,
	   );


if (!$ARGV[0]) {
    die "Usage: $0 recno...\n";
}



$persons = Counter::openfile("users");

for $key (@ARGV) {
    showuser($key);
}

sub showuser {
    my $clean = shift;

    print "============= Record $key ============\n";
    $user = $users->get($key);
    if (!$user) {
	print "No user record for this number\n";
    } else {
	$$user{email} = "--nothing--" if !$$user{email};
	$$user{logintime} = "--nothing--" if !$$user{logintime};
	$$user{emailtime} = "--nothing--" if !$$user{emailtime};
	printf "** %-9s: %s\n", "Replyto", $$user{email};
	printf "** state: %s\n", $$user{state}?$$user{state}:"<not set>";
	printf "** logintime: %s\n", $$user{logintime};
	printf "** emailtime: %s\n", $$user{emailtime};
	print "--------------------------------------------\n";
	$pers = $user->person();
	if (!$pers) {
	    print "No person entry for $key\n";
	} else {
	    $clean = $user->person("clean");
	    for $field ($clean->fields) {
		if (!defined($$clean{$field})) {
		    warn "Strange: field in clean->fields but undefined\n";
		}
		if (!defined($$pers{$field})) {
		    printf "%-12s: %s []\n", $field, $$clean{$field};
		} elsif ($$pers{$field} ne $$clean{$field}) {
		    printf "%-12s: %s (%s)\n", $field, $$clean{$field}, $$pers{$field};
		} else {
		    printf "%-12s: %s\n", $field,  $$pers{$field};
		}
	    }
	}
	my @emails = $user->emails();
	if (@emails) {
	    my $email;
	    print "Associated emails:\n";
	    for $email (@emails) {
		printf "%-5s %-32s %5s %-4s %.30s..\n",
		$$email{usage}, $email->key(), $$email{state},
		$$email{level}?$$email{level}:"<no level>",
		$$email{error}?$$email{error}:"";
	    }
	}
	my @machines = $user->machines();
	if (@machines) {
	    my $machine;
	    print "Machines:\n";
	    for $machine (@machines) {
		printf "%-6d %s\n", $machine->key(), 
		$$machine{name}?$$machine{name}:"<nameless>";
	    }
	}
    }
    if ($showhistory) {
	print "----- History ----\n";
	my ($ev, $field);
	my (@hist, %save);
	@hist = FileTable::Log::history("users", $key);
	push(@hist, FileTable::Log::history("persons", $key));
	for $ev (@hist) {
	    $$ev{date} = ".undated" if !$$ev{date};
	}
	for $ev (sort {$$a{date} cmp $$b{date}} @hist) {
	    print "$$ev{date}: $$ev{tablename} ($$ev{operation} $$ev{authority})\n";
	    printdiff($save{$$ev{tablename}}, $$ev{data}); 
	    $save{$$ev{tablename}} = $$ev{data};
	    print "\n";
	}
    }
}

sub printdiff {
    my ($old, $new) = @_;

    if (!$old) {
	$new =~ s/(^|\n)/$1N /g;
	print $new;
    } else {
	my @old = sort(split(/\n/, $old));
	my @new = sort(split(/\n/, $new));
	my @diffs = Algorithm::Diff::diff(\@old, \@new);
	my $hunk;
	for $hunk (@diffs) {
	    my $chg;
	    for $chg (@$hunk) {
		next if $$chg[2] =~ /^f_mtime/; # superfluous
		print $$chg[0], " ", $$chg[2], "\n",
	    }
	}
    }
}

	
